import { RequestModel } from '../../models/requestModel.js'
let md5 = require('../../utils/md5.js')
let requestModel=new RequestModel()
Page({
  data: {
    cardNumber:0,
    mobileNumber:0,
    userId:0,
    qrcodeImg:'',
    screenLight:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      cardNumber: options.cardNumber,
      mobileNumber: options.mobileNumber,
      userId: options.userId
    })
    let timestamp = (new Date()).valueOf()
    timestamp = parseInt(timestamp / 1000)
    let reqData = { "userId": this.data.userId }
    reqData['channel'] = 'Mobile'
    reqData['clientId'] = 'haiwaigou'
    reqData['terminalType'] = 'IOS'
    reqData['t'] = timestamp
    var sdic = Object.keys(reqData).sort()
    var pStr = ''
    for (var ki in sdic) {
      var dstr = reqData[sdic[ki]]
      pStr += sdic[ki] + '=' + dstr
      if (ki < sdic.length - 1) {
        pStr += '&'
      }
    }
    var h = md5.hexMD5(pStr)
    reqData['h'] = h
    var rurl = 'https://sns.tepin.com/sns/sso/v1/memberCardQRCode?h=' + reqData['h'] + '&terminalType=IOS&channel=Mobile&clientId=haiwaigou&userId=' + reqData['userId'] + '&t=' + reqData['t']
    this.setData({
      qrcodeImg: rurl
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    requestModel.getScreenBrightness()
      .then(res => {
        this.setData({
          screenLight: res
        })
      })
    requestModel.setScreenBrightness(1)
      .then(res => {
        console.log(res.errMsg)
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let screenLight = this.data.screenLight
    //console.log(screenLight)
    requestModel.setScreenBrightness(screenLight)
      .then(res => {
        //console.log(res.errMsg)
      })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})